package main

import(
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
	"go-practice/面向对象/类型系统/音乐播放/library"
	"go-practice/面向对象/类型系统/音乐播放/mp"
)
var lib library.MusicManager
var id int = -1

func handleLibCommands(tokens []string) {
	switch tokens[1]{
		case "list":
			//defer fmt.Println(lib.Len())
			//if lib.Len() <= 0 {
			//	fmt.Println("hello world")
			//	return
			//}
			for i := 0; i < lib.Len(); i++ {
				e, _ := lib.Get(i)
				fmt.Println(i+1, ":", e.Id, e.Name, e.Artist, e.Source, e.Type)
			}
		case "add":
			if len(tokens) == 6{
			 	id++
				fmt.Println("id is", id)
			 	lib.Add(&library.Music{
			 		strconv.Itoa(id), tokens[2], tokens[3], tokens[4], tokens[5] })
			}else{
			 	fmt.Println("USAGE: lib add <name><artist><source><type>")
			}
		case "remove":
			if len(tokens) == 3 {
				lib.RemoveByName(tokens[2])
			}else{
				fmt.Println("USAGE: lib remove <name>")
			}
		default: 	
			fmt.Println("Unrecognized lib command : ", tokens[1])
	}
}

func handlePlayCommand(tokens []string)  {
	if len(tokens) != 2 {
		fmt.Println("USAGR: play <name>")
		return
	}
	e := lib.Find(tokens[1])
	if e == nil {
		fmt.Println("The mucis", tokens[1], "does not exist")
		return
	}
	mp.Play(e.Source, e.Type)
}

func main() {
	fmt.Println(`
			Enter following commands to control the player:
			lib list -- View the existing music lib
			lib add <name><artist><source><type> -- Add a music to the music lib
			lib remove <name> -- Remove the specified music from the lib
			play <name> -- Play the specified music
	`)
	//lib := library.NewMusicManager()
	r := bufio.NewReader(os.Stdin)

	for  {
		fmt.Println("Enter command ->")
		rawLine, _, _ := r.ReadLine()
		line := string(rawLine)
		if line == "q" || line == "e" {
			break
		}
		tokens := strings.Split(line, " ")
		//for a, b := range tokens{
		//	fmt.Println(a)
		//	fmt.Println(b)
		//}
		if tokens[0] == "lib" {
			handleLibCommands(tokens)
		}else if tokens[0] == "play"{
			handlePlayCommand(tokens)
		}else{
			fmt.Println("Unrecognized command", tokens[0])
		}
	}
}
